<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');

need_login();
$condition = array( 'user_id' => $login_user_id, 'team_id > 0', );
$selector = strval($_GET['s']);
$allow = array('index','unpay','pay','askrefund');
$cur_foot = 'myorder';

if (false==in_array($selector, $allow))  $selector == 'index';

if ( $selector == 'index' ) {
	
}
else if ( $selector == 'unpay' ) {
	$condition['state'] = 'unpay';
}
else if ( $selector == 'pay' ) {
	$condition['state'] = 'pay';
	$condition['is_get'] = 0;
}
else if ( $selector == 'askrefund' ) {
	$condition['state'] = 'pay';
	$condition['allowrefund'] = 'Y';
}
if(isset($INI['system']['secsite']) && !empty($INI['system']['secsite']))
	$cur_url = $INI['system']['wwwprefix'].$INI['system']['secsite'].'/order/index.php?s='.$selector;
else 
	$cur_url = $INI['system']['wwwprefix'].'/order/index.php?s='.$selector;



$count = Table::Count('order', $condition);

list($pagesize, $offset, $pagestring) = pagestring($count, 10);

$orders = DB::LimitQuery('order', array(
	'condition' => $condition,
	'order' => 'ORDER BY  id desc',
	'size' => $pagesize,
	'offset' => $offset,
));

$team_ids = Utility::GetColumn($orders, 'team_id');
$teams = Table::Fetch('team', $team_ids);
foreach($teams AS $tid=>$one){
	team_state($one);
	$teams[$tid] = $one;
}

if(isset($_GET['page'])  && intval($_GET['page']) > 1)
{
	$s_html = '';
	$result = array('ret'=>1,'offset'=>$offset);
	if(!empty($orders)){
		$result['ret'] = 0;
	}
	foreach($orders as $index =>$one)
	{
		
		$s_html .= '<div class="order" id="orderdel_'.$one['id'].'">';
		$s_html .= '	<div class="order_hd">';
		$s_html .= date('Y-m-d H:i:s',$one['create_time']);
		$s_html .= 	'</div>';
		$s_html .= 	'	<div class="order_bd">';
		$s_html .= 	'		<div class="order_glist">';
		$s_html .= 	'			<a href="/order/view.php?id='.$one['id'].'">';
		$s_html .= 	'				<div class="order_goods">';
		$s_html .= 	'					<div class="order_goods_img"> '; 
		$s_html .= 	'						<img alt="" src="'.team_image($teams[$one['team_id']]['image']).'">';
		$s_html .= 	'					</div>';
		$s_html .= 	'					<div class="order_goods_info">';
		$s_html .= 	'						<div class="order_goods_name">'.$teams[$one['team_id']]['title'].'</div>';
		$s_html .= 	'						<div class="order_goods_attr">';
		$s_html .= 	'							<div class="order_goods_attr_item">';
		$s_html .= 	'								<div class="order_goods_price"><i>¥</i>'.moneyit($one['origin']).'<i>/件</i></div>数量：'.$one['quantity'];
		$s_html .= 	'							</div>';
		$s_html .= 	'						</div>';
		$s_html .= 	'					</div>';
		$s_html .= 	'				</div>';
		$s_html .= 	'			</a>';
		$s_html .= 	'			<div class="order_ft">';
		$s_html .= 	'				<div class="order_total">';
		$s_html .= 	'					<span class="order_total_info">共'.$one['quantity'].'件商品 ，免运费</span>';
		$s_html .= 	'					<span class="order_price">实付：<b>¥'.moneyit($one['origin']).'</b></span>';
		$s_html .= 	'					<span class="coupon_icon" ></span>';
		$s_html .= 	'				</div>';
						
						
						if($one['state']=='unpay'&&$teams[$one['team_id']]['close_time']==0)
						{
							
							
							if( $one['rstate']=='berefund')
							{
								$s_html .= 	'<div class="order_opt">';
								$s_html .= 	'	<span class="order_status">已退款.</span>';
								$s_html .= 	'</div>';
							} else {
								$s_html .= 	'<div class="order_opt">';
								$s_html .= 	'	<span class="order_status">待支付</span>';
								$s_html .= 	'	<div class="order_btn">';
								$s_html .= 	'		<a class="order_btn_buy" href="/order/check.php?id='.$one['id'].'">去支付</a>';
								$s_html .= 	'		<a onclick="orderdel('.$one['id'].')" href="javascript:;" >取消订单</a>';
								$s_html .= 	'	</div>';
								$s_html .= 	'</div>';
							}
						
						
						} else if($one['state']=='pay')
						{
						
						
						
						
						$s_html .= 	'<div class="order_opt">';
						
						if ($one['express_id']&&$one['express_no'])
						{
							$s_html .= 	'	<span class="order_status">已支付,已发货</span>';
						} else {
							$s_html .= 	'	<span class="order_status">已支付,请耐心等待发货</span>';
						}
						
						
						$s_html .= 	'	<div class="order_btn">';
						$s_html .= 	'		<a class="order_btn_receive" href="/order/view.php?id='.$one['id'].'">查看物流</a>';
						
						if($one['is_get']==1)
						{
							$s_html .= 	'<span class="order_status" style="margin-left:5px;">已收货</span>';
						}
						else 
						{
							$s_html .= 	'<a class="order_btn_receive " href="javascript:;" id="get_exp'.$one['id'].'" onclick="can_get_express('.$one['id'].')"  >确认收货</a>';
							$s_html .= 	'<span class="order_status" style="margin-left:5px;display:none;" id="get_exp_s'.$one['id'].'" >已收货</span>';
						}
							
						
						$s_html .= 	'	</div>';
						$s_html .= 	'</div>';
						}
						else if( $teams[$one['team_id']]['close_time']>0)
						{
						
						$s_html .= 	'<div class="order_opt">';
						$s_html .= 	'	<span class="order_status">已过期</span>';
						$s_html .= 	'	<div class="order_btn">';
						$s_html .= 	'		<a onclick="orderdel('.$one['id'].')" href="javascript:;" >删除</a>';
						$s_html .= 	'	</div>';
						$s_html .= 	'</div>';
						}
						
						
			$s_html .= 	'		</div>';
			$s_html .= 	'	</div>';
			$s_html .= 	'</div>';
			$s_html .= 	'</div>';
		
	}
	
	$result['html'] = $s_html;
	echo json_encode($result);
	die();
}


$pagetitle = '我的订单';
include template('order_index');
